/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-19
 * Time: 22:14
 */
public class Solution {
    static class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;

        }
    }
    public TreeNode prev = null;
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null) return null;
        ConvertChild(pRootOfTree);
        TreeNode head = pRootOfTree;
        while(head.left != null) {
            head = head.left;
        }
        return head;
    }

    public void ConvertChild(TreeNode root) {
        if(root == null) return;
        ConvertChild(root.left);
        root.left = prev;
        if(root.left != null) {
            prev.right = root;
        }
        prev = root;
        ConvertChild(root.right);
    }
}
